package com.ptswitch.material.pages.user;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.LoginInfoService;
import com.ptswitch.material.service.UserService;
import com.ptswitch.material.util.HlogConstants;

public class DeleteUser extends MaterialBasePage {

    @Inject
    private UserService userSer;
    @Inject
    private LoginInfoService loginInfoSer;
    @InjectPage
    private ListUser listUser;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object delete(String id) {
        checkUserIsAdmin();
        Uzer user = userSer.findById(id);
        try {
            if (user != null) {
                userSer.delete(user);
                loginInfoSer.deleteByUsername(user.getUsername());
                hlog(HlogConstants.deleteUser, user);
            }
        } catch (Exception e) {
            setMessage(e.getMessage());
        }
        return listUser;
    }
}
